package com.mojianxi.ad.sender.index;

import com.alibaba.fastjson.JSON;
import com.mojianxi.ad.dump.table.*;
import com.mojianxi.ad.handler.AdLevelDataHandler;
import com.mojianxi.ad.index.DataLevel;
import com.mojianxi.ad.mysql.constant.Constant;
import com.mojianxi.ad.mysql.dto.MySqlRowData;
import com.mojianxi.ad.sender.ISender;
import com.mojianxi.ad.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author:liuxiaodong
 * @Date 2019/11/6 14:17
 * @Company dyld
 */
@Slf4j
@Component("indexSender")
public class IndexSender implements ISender {

    @Override
    public void sender(MySqlRowData rowData) {
        String level=rowData.getLevel();
        if(DataLevel.LEVEL2.getLevel().equals(level)){
            level2RowData(rowData);
        }else if(DataLevel.LEVEL3.equals(level)){
            level3RowData(rowData);
        }else if(DataLevel.LEVEL4.equals(level)){
            level4RowData(rowData);
        }else {
            log.error("MysqlRowData ERROR:{}", JSON.toJSONString(rowData));
        }
    }
    private void level2RowData(MySqlRowData rowData){
        if(rowData.getTableName().equals(Constant.AD_PLAN_TABLE_INFO.TABLE_NAME)){
            List<AdPlanTable> planTables=new ArrayList<>();
            for(Map<String,String> fieldValueMap:rowData.getFieldValueMap()){
                AdPlanTable planTable=new AdPlanTable();
                fieldValueMap.forEach((k,v) -> {
                    switch (k){
                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_ID:
                            planTable.setId(Long.valueOf(v));
                            break;
                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_USER_ID:
                            planTable.setUserId(Long.valueOf(v));
                            break;
                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_PLAN_STATUS:
                            planTable.setPlanStatus(Integer.valueOf(v));
                            break;
                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_START_DATE:
                            planTable.setStartDate(CommonUtils.parseStringDate(v));
                            break;
                        case Constant.AD_PLAN_TABLE_INFO.COLUMN_END_DATE:
                            planTable.setEndDate(CommonUtils.parseStringDate(v));
                            break;
                    }
                });
                planTables.add(planTable);
            }
            planTables.forEach(p -> AdLevelDataHandler.handleLevel2(p,rowData.getOpType()));
        }else if(rowData.getTableName().equals(Constant.AD_CREATIVE_TABLE_INFO.TABLE_NAME)){
            List<AdCreativeTable> creativeTables=new ArrayList<>();
            for(Map<String,String> fieldValueMap:rowData.getFieldValueMap()){
                AdCreativeTable creativeTable=new AdCreativeTable();
                fieldValueMap.forEach((k,v) -> {
                    switch (k){
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_ID:
                            creativeTable.setAdId(Long.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_TYPE:
                            creativeTable.setType(Integer.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_MATERIAL_TYPE:
                            creativeTable.setMaterialType(Integer.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_HEIGHT:
                            creativeTable.setHeight(Integer.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_WIDTH:
                            creativeTable.setWidth(Integer.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_AUDIT_STATUS:
                            creativeTable.setAuditStatus(Integer.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_TABLE_INFO.COLUMN_URL:
                            creativeTable.setAdUrl(v);
                            break;
                    }
                });
                creativeTables.add(creativeTable);
            }
            creativeTables.forEach(c -> AdLevelDataHandler.handleLevel2(c,rowData.getOpType()));
        }
    }
    private void level3RowData(MySqlRowData rowData){
        if(rowData.getTableName().equals(Constant.AD_UNIT_IT_TABLE_INFO.TABLE_NAME)){
            List<AdUnitTable> unitTables=new ArrayList<>();
            for(Map<String,String> fieldValueMap:rowData.getFieldValueMap()){
                AdUnitTable unitTable=new AdUnitTable();
                fieldValueMap.forEach((k,v) -> {
                    switch (k){
                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_ID:
                            unitTable.setUnitId(Long.valueOf(v));
                            break;
                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_UNIT_STATUS:
                            unitTable.setUnitStatus(Integer.valueOf(v));
                            break;
                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_POSITION_TYPE:
                            unitTable.setPositionType(Integer.valueOf(v));
                            break;
                        case Constant.AD_UNIT_TABLE_INFO.COLUMN_PLAN_ID:
                            unitTable.setPlanId(Long.valueOf(v));
                            break;
                    }
                });
                unitTables.add(unitTable);
            }
            unitTables.forEach(p -> AdLevelDataHandler.handleLevel3(p,rowData.getOpType()));
        }else if(rowData.getTableName().equals(Constant.AD_CREATIVE_UNIT_TABLE_INFO.TABLE_NAME)){
            List<AdCreativeUnitTable> creativeUnitTables=new ArrayList<>();
            for(Map<String,String> fieldValueMap:rowData.getFieldValueMap()){
                AdCreativeUnitTable creativeUnitTable=new AdCreativeUnitTable();
                fieldValueMap.forEach((k,v) -> {
                    switch (k){
                        case Constant.AD_CREATIVE_UNIT_TABLE_INFO.COLUMN_CREATIVE_ID:
                            creativeUnitTable.setAdId(Long.valueOf(v));
                            break;
                        case Constant.AD_CREATIVE_UNIT_TABLE_INFO.COLUMN_UNIT_ID:
                            creativeUnitTable.setUnitId(Long.valueOf(v));
                            break;
                    }
                });
                creativeUnitTables.add(creativeUnitTable);
            }
            creativeUnitTables.forEach(c -> AdLevelDataHandler.handleLevel3(c,rowData.getOpType()));
        }
    }
    private void level4RowData(MySqlRowData rowData){
        if(rowData.getTableName().equals(Constant.AD_UNIT_DISTRICT_TABLE_INFO.TABLE_NAME)){
            List<AdUnitDistrictTable> unitDistrictTables=new ArrayList<>();
            for(Map<String,String> fieldValueMap:rowData.getFieldValueMap()){
                AdUnitDistrictTable unitDistrictTable=new AdUnitDistrictTable();
                fieldValueMap.forEach((k,v) -> {
                    switch (k){
                        case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_UNIT_ID:
                            unitDistrictTable.setUnitId(Long.valueOf(v));
                            break;
                        case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_PROVINCE:
                            unitDistrictTable.setProvince(v);
                            break;
                        case Constant.AD_UNIT_DISTRICT_TABLE_INFO.COLUMN_CITY:
                            unitDistrictTable.setCity(v);
                            break;
                    }
                });
                unitDistrictTables.add(unitDistrictTable);
            }
            unitDistrictTables.forEach(p -> AdLevelDataHandler.handleLevel4(p,rowData.getOpType()));
        }else if(rowData.getTableName().equals(Constant.AD_UNIT_IT_TABLE_INFO.TABLE_NAME)){
            List<AdUnitItTable> unitItTables=new ArrayList<>();
            for(Map<String,String> fieldValueMap:rowData.getFieldValueMap()){
                AdUnitItTable unitItTable=new AdUnitItTable();
                fieldValueMap.forEach((k,v) -> {
                    switch (k){
                        case Constant.AD_UNIT_IT_TABLE_INFO.COLUMN_UNIT_ID:
                            unitItTable.setUnitId(Long.valueOf(v));
                            break;
                        case Constant.AD_UNIT_IT_TABLE_INFO.COLUMN_IT_TAG:
                            unitItTable.setItTag(v);
                            break;
                    }
                });
                unitItTables.add(unitItTable);
            }
            unitItTables.forEach(c -> AdLevelDataHandler.handleLevel4(c,rowData.getOpType()));
        }else if(rowData.getTableName().equals(Constant.AD_UNIT_KEYWORD_TABLE_INFO.TABLE_NAME)){
            List<AdUnitKeywordTable> unitKeywordTables=new ArrayList<>();
            for(Map<String,String> fieldValueMap:rowData.getFieldValueMap()){
                AdUnitKeywordTable unitKeywordTable=new AdUnitKeywordTable();
                fieldValueMap.forEach((k,v) -> {
                    switch (k){
                        case Constant.AD_UNIT_KEYWORD_TABLE_INFO.COLUMN_UNIT_ID:
                            unitKeywordTable.setUnitId(Long.valueOf(v));
                            break;
                        case Constant.AD_UNIT_KEYWORD_TABLE_INFO.COLUMN_KEYWORD:
                            unitKeywordTable.setKeyword(v);
                            break;
                    }
                });
                unitKeywordTables.add(unitKeywordTable);
            }
            unitKeywordTables.forEach(c -> AdLevelDataHandler.handleLevel4(c,rowData.getOpType()));
        }
    }
}
